home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / message.zip / ARTICLE.ASC next >
Text File  |  1992-11-10  |  23KB  |  496 lines

  1.                         MESSAGE MINDER
  2.  
  3.      Windows 3.x is all the rage these days, and it seems
  4. that plain old DOS has become passe, right? Well, there has
  5. never been a more inaccurate statement. I use DOS
  6. exclusively, as does most everyone else I know. The latest
  7. Windows craze also makes it difficult for new programmers to
  8. "catch up" to top notch programmers because all the journals
  9. are singing the praises of Windows, with little attention
  10. paid to programming in DOS.  I am a college student speaking
  11. from experience here.  I found it necessary to go through
  12. magazines five years old to catch up to what people are
  13. talking about in the various programming journals published
  14. today!
  15.      The most difficult topic of DOS programming was also the
  16. most difficult thing for me to find information on: Terminate
  17. and Stay Resident programs, or TSRs.  Another area which many
  18. programmers find daunting is dealing with the PC's serial
  19. port. Well, here is a program I wrote which deals with BOTH
  20. concepts.  If TSR programming is old hat to you, perhaps you
  21. might find the code which uses the serial port of interest.
  22.  
  23.                 THE PROGRAM: What does it do?
  24.  
  25.      When I decided to undertake the creation of a TSR
  26. utility, I didn't want another "me too" note pad or it's
  27. ilk; I wanted to create something I hadn't seen before.  My
  28. program, simply called "message", will allow messages to be
  29. exchanged between two computers connected by their serial
  30. ports with a null modem cable. If "message" is loaded on both
  31. machines, a two-way conversation can take place. No longer
  32. would it be necessary to walk from your office to a friend's
  33. to remind her of a lunch date, or to ask for quick advice.
  34.      Once "message" is loaded it records the message strings
  35. from the sending computer and displays them in a window at
  36. the upper left corner of the screen when the entire message
  37. is received.  It will then pause until the user presses a
  38. key.  To send a message the user would press the hot-key
  39. <ctrl> w, resulting in a window being displayed at the top of
  40. the screen.  Type in your message here, followed by return.
  41.      If you would prefer to load "message" on only one
  42. machine, messages could (optionally) be sent by the quick and
  43. dirty program I wrote called "send".  It's use could not be
  44. simpler:
  45.  
  46.     C:\send Well, do you think Clinton will win?
  47.  
  48. Send works by taking each command line argument (the words of
  49. your message) and copying them into a single string. The
  50. string is then sent out the serial port, one character at a
  51. time.
  52.  
  53.  
  54.  
  55.      To un-install the program the user would enter the
  56. following:
  57.  
  58.      C:\message u
  59.  
  60.  
  61.  
  62.           THE SERIAL PORT: It isn't really THAT bad!
  63.  
  64.      OK! Before delving into how to make a TSR I feel
  65. compelled by guilt to talk a little about the serial port.
  66. PLEASE NOTE that this article is about TSR programming, and
  67. the information provided on the serial port is intended to
  68. give you an idea of how that portion of the program
  69. functions.
  70.      Two things in the program listing that you should be
  71. looking at are 1) the serial() function in "Message", and
  72. 2) the "Send" program.
  73.      If you don't really care how the serial port works you
  74. still need to know how to connect two machines together for
  75. "Message" to work. You can let someone rip you off by paying
  76. far too much for a null modem connector, or you can be frugal
  77. and make something yourself. Here is how:
  78.      Serial ports come in two flavors:  9 pin connectors and
  79. 25 pin connectors. Both serve the same function, the 9 pin
  80. just eliminates the unnecessary connections. In order for
  81. "Message" to work you will only need to run two wires between
  82. the two machines. The other wires are there to communicate
  83. with your modem so "Message" does not need them.
  84.      On a 25 pin connector pin 2 is used to transmit data,
  85. and pin 3 is used to receive data; on a 9 pin connector pin 2
  86. is used to receive data and pin 3 is used to transmit data.
  87. Two connect two PCs with the same type of serial connector
  88. together, run a wire from pin 2 of each machine to pin 3 of
  89. the other machine. I also recommend that you connect the
  90. ground pins (pin 7 on a 25 pin connector) of each PC
  91. together.  "Message" will usually work without connecting the
  92. ground pins, but it is a good (and safe) practice to connect
  93. them.
  94.      Connectivity aside, how does one send and receive
  95. characters with the serial port? One possible method is the
  96. use of the BIOS, but it is far too slow. For any real speed
  97. we must directly use the PCs UART. UART stands for Universal
  98. Asynchronous Receiver-Transmitter.
  99.      The UART is a chip inside of your computer, generally
  100. the 8250. You talk to this chip via port addresses. A port
  101. address is similar to a memory location except it is
  102. connected to some external hardware.  The base port address
  103. of the PCs first serial card (COM 1) is usually 3F8 hex.
  104. Like your microprocessor, the 8250 has internal registers.
  105. You access these registers by their port address. 3F8 is
  106. called the base address because it is the location of the
  107. first register. The second register would be located at port
  108. 3F9, etc...
  109.                 THE WORKINGS OF A TSR PROGRAM.
  110.  
  111.      The first thing "message" does when it is loaded is to
  112. determine how much memory it occupies. This information will
  113. be saved so that when the program terminates it can tell DOS
  114. how much memory to reserve for it.
  115.      Before we undertake the task of determining the size of
  116. the program a word or two should be said about the PSP, or
  117. Program Segment Prefix.  When DOS loads a program into memory
  118. it creates a 256 byte block of memory containing information
  119. about that program. This block of memory always starts at
  120. offset 0000.  As assembly language programmers know, code
  121. usually starts after the PSP at offset 100h,  256 decimal.
  122.  
  123.      In Microsoft C there exists a global variable _psp which
  124. contains the segment address of the PSP of the current
  125. program. With this information we now know where in memory
  126. "message" begins.  "Message" takes advantage of this right
  127. away by setting the huge pointer tsrbottom to point to
  128. _psp:0000.  This address is where our program starts. A huge
  129. pointer is used so that C will do our pointer arithmetic for
  130. us.
  131.  
  132.      Programs created with Microsoft C in the small memory
  133. model are followed in memory by their stack space, then the
  134. heap space used for run-time memory allocation.  NOTE -
  135. Borland swaps the heap and the stack area! With Microsoft C
  136. the address pointed to by SP:SS plus a maximum heap size is
  137. an effective estimate for the end of the program;  For
  138. Borland the end of your program is simply SP:SS.  To obtain
  139. this information you must have access to register values.
  140. "Message" accomplishes this by using the Microsoft _asm
  141. keyword.  The following lines , taken from "Message", will
  142. copy the stack address into the huge character pointer
  143. tsrstack:
  144.  
  145.   137  _asm mov WORD PTR tsrstack[0], sp  // save stack ptr
  146.   138  _asm mov WORD PTR tsrstack[2], ss  // and  stack seg
  147.  
  148.        Then establish a far pointer to our PSP.
  149.  
  150.   139  FP_SEG(tsrbottom) = _psp;          // save PSP segment
  151.   140  FP_OFF(tsrbottom) = 0;             // offset of 0
  152.  
  153.      To obtain the program size in bytes simply subtract the
  154. PSP address from the stack address, and add to this byte
  155. value to maximum amount of memory you will allocate with
  156. malloc(). DOS expects this value in paragraphs, not bytes.
  157. This makes it necessary to divide our byte size by 16. This
  158. can easily be accomplished by shifting the byte value 4 bits
  159. to the right (2 ^ 4 = 16).
  160. Here is how "Message" does it:
  161.  
  162.   141  tsrsize = ((tsrstack - tsrbottom) >> 4) + 1;
  163.      Another useful consequence of saving our stack address
  164. is that when our TSR is activated we can use our own stack,
  165. rather than using the stack of the interrupted process. This
  166. is beneficial because we have no way of knowing how much
  167. stack space is left in the process we are interrupting. The
  168. doit() function shows how setting up the stack can be
  169. accomplished.
  170.  
  171.      Whew! The next procedure "Message" undertakes is the
  172.